package com.liangzhong.proxy.PlayerCenter
{
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.commandconst.playercenter.SMDPlayerCenter;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.playerCenter.PlayerCenterDataManager;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.PlayerCenter.PlayerCenterConstant;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 *球员中心 
	 * @author Administrator
	 * 
	 */	
	public class PlayerCenterProxy extends BaseProxy
	{
		/**
		 *俱乐部proxy名字 
		 */
		public static const PROXYNAME:String = "PlayerCenterProxy_proxyname";
		
		
		
		/**
		 * 
		 */
		private var _playerCenterData:PlayerCenterDataManager;
		public function PlayerCenterProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
			_playerCenterData = AppData.getInstance().playerCenterDataManager;
		}
		/**
		 *接受 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case MainCommandType.MAINDEAL:
				{
					return onDeal(subCmdID,buffer,dataSize);
				}
				case MainCommandType.PUBLICCOMMANDTYPE:
				{
					return onPlayerPower(subCmdID,buffer,dataSize);
				}
				case MainCommandType.MATCH_RESULE:
				{
					onMatchResult(subCmdID,buffer,dataSize);
				}
					
					
			}
			return false;
		}
		private function onMatchResult(subCmdID:uint,
									   buffer:ByteArray, 
									   dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDPlayerCenter.CHANGE_BUFF:
					var len:int = buffer.readShort();
					var playerId:int;
					var status:int
					for(var i:int = 0;i < len;i++)
					{
						playerId = buffer.readInt();
						status = buffer.readShort();
						_playerCenterData.updataPlayerBuff(playerId,status);
					}
					
					
					return true;
			}
			return false;
		}
		/**
		 * 球员自动改变体力 
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 * 
		 */		
		private function onPlayerPower(subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDPlayerCenter.AUTO_PLAYER_POWER:
					var nretrun:int = buffer.readInt();
					var len:int = buffer.readShort();
					for(var i:int = 0;i < len;i++)
					{
						var playerId:int = buffer.readInt();
						var status:int = buffer.readShort();
						_playerCenterData.updataPlayerPower(playerId,status);
					}
					
					return true;
			}
			return false;
		}
		/**
		 *处理 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDPlayerCenter.PLAYERCENTER_UP:
				{
					getUgGrade(buffer);
					return true;
				}
				case SMDPlayerCenter.REPLY_BUFF:
				{
					getReplyBuff(buffer)
					return true;
				}
				case SMDPlayerCenter.REPLY_POWER://回复体力
				{
					getReplyPower(buffer);
					return true;
				}
				case SMDPlayerCenter.STRONG_REPLY_POWER://强力回复体力
				{
					STRONGReplyPower(buffer);
					return true;
				}
				case SMDPlayerCenter.RETIREPLAYER://退役
					getretirePlayer(buffer);
					return true;
				case SMDPlayerCenter.LAYOFFPLAYER://解雇
					getlayoffPlayer(buffer);
					return true;
				case SMDPlayerCenter.ADDMORAL://增加士气 
					getAddMoral(buffer);
					return true;
				case SMDPlayerCenter.SAVE_BATTLEARRAY:
					getbattlearray(buffer);
					return true;
				case SMDPlayerCenter.REPLACE_EQUIP:
					getreplace_equip(buffer);
					return true;
				case SMDPlayerCenter.CHANGE_PLAYER_NUMBER:
					getChangePlayerNumber(buffer);
					return true;
				case SMDPlayerCenter.ADD_PLAYER_CELL:
					getaddPlayerCell(buffer);
					return true;
			}
			return false;
		}
		/**
		 *请求球员中心升级  
		 * 
		 */
		public function requestUpGrade($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,MainCommandType.MAINDEAL,SMDPlayerCenter.PLAYERCENTER_UP);
//			var id:int = $obj.id;
//			sendBuf.writeInt(id);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回球员中心升级 
		 * 
		 */
		private function getUgGrade(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
			if(nretrun ==0)
			{
				_playerCenterData.playerCenterVo.level = buffer.readByte();
				_playerCenterData.playerCenterVo.coin = buffer.readInt();
				_playerCenterData.playerCenterVo.needlevel = buffer.readShort();
				_playerCenterData.playerCenterVo.players = buffer.readShort();
				var curtime :Number = AppData.getInstance().time;
				_playerCenterData.playerCenterVo.pretime = curtime;
//				switch(_playerCenterData.playerCenterVo.level)
//				{
//					case 3:
//					case 6:
//					case 9:
//						_playerCenterData.playerCenterUpUpdataPlayerphysical();
//					break;
//				}
				this.sendNotification(PlayerCenterConstant.GET_PLAYERCENTER_UPGRADE,{status:true});
				Alert.showErrorTip("升级成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
				this.sendNotification(PlayerCenterConstant.GET_PLAYERCENTER_UPGRADE,{status:false});
				
			}
		}
		/**
		 *请求冷却时间 
		 * @param $obj
		 * 
		 */
		public function requestClearUpTime($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,MainCommandType.MAINDEAL,CMD_Logic.DROP_BUILDING_Time);
			sendBuf.writeByte(BuildingType.PLAYERCENTER);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回冷却时间 
		 * 
		 */
		public function getClearUpTime($obj:Object):void
		{
			Alert.showErrorTip("时间冷却成功");
			_playerCenterData.playerCenterVo.pretime = 0;
			
//			this.sendNotification(PlayerCenterConstant.GET_CLEAR_UP_TIME);
		}
		/**
		 *请求回复buff
		 * @param $obj
		 * 
		 */
		public function requestReplyBuff($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 6,MainCommandType.MAINDEAL,SMDPlayerCenter.REPLY_BUFF);
			sendBuf.writeInt($obj.id);
			sendBuf.writeShort($obj.cellId);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回回复buff 
		 * 
		 */
		public function getReplyBuff(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
//			trace(nretrun);
			if(nretrun ==0)
			{
				var playerId:int = buffer.readInt();
				var status:int = buffer.readShort();
				_playerCenterData.updataPlayerBuff(playerId,status);
				this.sendNotification(PlayerCenterConstant.GET_CHANGE_POWERANDBUFF,{status:true,type:1,id:playerId});
				Alert.showErrorTip("回复成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
				this.sendNotification(PlayerCenterConstant.GET_CHANGE_POWERANDBUFF,{status:false});
				
			}	
		}
		/**
		 *请求回复体力 
		 * type :1为单个回复 2为全部回复
		 * @param $obj
		 * 
		 */
		public function requestReplyPower($obj:Object):void
		{
			var sendBuf :ByteArray
			var comm:int;
			if($obj.type == 1)
			{
				comm = SMDPlayerCenter.REPLY_POWER;
				sendBuf = 
					CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 6,MainCommandType.MAINDEAL, SMDPlayerCenter.REPLY_POWER );
				sendBuf.writeInt($obj.id);
				sendBuf.writeShort($obj.cellId);
			}else
			{
				sendBuf = 
					CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 2,MainCommandType.MAINDEAL, SMDPlayerCenter.STRONG_REPLY_POWER );
				sendBuf.writeShort($obj.cellId);
			}
			
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		
		/**
		 *返回回复体力 
		 * 
		 */
		public function getReplyPower(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
			if(nretrun ==0)
			{
				var playerId:int = buffer.readInt();
				var status:int = buffer.readShort();
				_playerCenterData.updataPlayerPower(playerId,status);
				this.sendNotification(PlayerCenterConstant.GET_CHANGE_POWERANDBUFF,{status:true,type:2,id:playerId});
				Alert.showErrorTip("回复成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
				this.sendNotification(PlayerCenterConstant.GET_CHANGE_POWERANDBUFF,{status:false});
				
			}
		}
		/**
		 * 强力回复体力
		 * 
		 */
		private function STRONGReplyPower(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
			if(nretrun ==0)
			{
				_playerCenterData.updataPlayerPower();
				this.sendNotification(PlayerCenterConstant.GET_CHANGE_POWERANDBUFF,{status:true,id:0});
				Alert.showErrorTip("回复成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
				this.sendNotification(PlayerCenterConstant.GET_CHANGE_POWERANDBUFF,{status:false});
				
			}
		}
		/**
		 *请求增加士气  
		 * @param $obj
		 * 
		 */
		public function requestAddMoral($obj:Object):void
		{
//			this.sendNotification(PlayerCenterConstant.GET_ADD_MORMAL,{status:true});
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 2,MainCommandType.MAINDEAL,SMDPlayerCenter.ADDMORAL);
			sendBuf.writeShort($obj.cellId);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回增加士气  
		 * 
		 */
		private function getAddMoral(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
			if(nretrun ==0)
			{
				var i:int = buffer.readByte()
				AppData.getInstance().clubDatamanager.clubInfo.morale = i;
				this.sendNotification(PlayerCenterConstant.GET_ADD_MORMAL,{status:true});
				//Alert.showErrorTip("士气增加");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
				this.sendNotification(PlayerCenterConstant.GET_ADD_MORMAL,{status:false});
				
			}
		}
		
		/**
		 *解雇球员 
		 *  1为退役
		 * 2为解雇  
		 * @param $obj
		 * 
		 */
		public function requestRemovePlayer($obj:Object):void
		{
//			_playerCenterData.removePlayerVo($obj.id);
//			this.sendNotification(PlayerCenterConstant.GET_REMOVE_PLAYER,{status:true,type:2,id:$obj.id});
//			return;
			var type:int = $obj.type;
			var comm:int;
			if(type == 1)
			{
				comm = SMDPlayerCenter.RETIREPLAYER;
			}else
			{
				comm = SMDPlayerCenter.LAYOFFPLAYER
			}
			
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,MainCommandType.MAINDEAL,comm);
			var id:int = $obj.id;
			sendBuf.writeInt(id);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		
		/**
		 *返回退役球员 
		 * 
		 */
		private function getretirePlayer(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
			var id:int;
			if(nretrun ==0)
			{
				id = buffer.readInt();
				_playerCenterData.removePlayerVo(id);
//				removePlayerGetEquip(buffer);
				this.sendNotification(PlayerCenterConstant.GET_REMOVE_PLAYER,{status:true,type:1,id:id});
				Alert.showErrorTip("退役成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
				this.sendNotification(PlayerCenterConstant.GET_REMOVE_PLAYER,{status:false,type:1});
				
			}
		}
		
		/**
		 *返回解雇球员 
		 * 
		 */
		private function getlayoffPlayer(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
			var id:int;
			if(nretrun ==0)
			{
				id = buffer.readInt();
				_playerCenterData.removePlayerVo(id);
//				removePlayerGetEquip(buffer);
				this.sendNotification(PlayerCenterConstant.GET_REMOVE_PLAYER,{status:true,type:2,id:id});
				Alert.showErrorTip("解雇成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
				this.sendNotification(PlayerCenterConstant.GET_REMOVE_PLAYER,{status:false,type:2});
				
			}
		}
		/**
		 *解雇和退役球员身上装备返回背包 
		 * 
		 */
		private function removePlayerGetEquip(buffer:ByteArray):void
		{
			var len		:int;
			var celled:int;//格子id
			var pid		:int;//物品ID
			var type	:int;
			var locked	:int;
			var id		:int;
			len = buffer.readShort();
			for(var i:int = 0;i < len;i++)
			{
				celled 	= buffer.readShort();
				pid		= buffer.readUnsignedInt();
				type 	= buffer.readByte();
				locked	= buffer.readByte();
				AppData.getInstance().bagDataManager.addPropByTypeAndId(celled,type,pid,locked);
			}
		}
		/**
		 *请求增加球员格子 
		 * 
		 */
		public function requestaddPlayerCell():void
		{
//			this.sendNotification(PlayerCenterConstant.GET_ADD_PLAYER_CELL,{status:true});
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size ,MainCommandType.MAINDEAL,SMDPlayerCenter.ADD_PLAYER_CELL);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回 获得球员格子
		 * @param buffer
		 * 
		 */
		private function getaddPlayerCell(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
			var id:int;
			if(nretrun ==0)
			{
				_playerCenterData.playerCenterVo.players = buffer.readShort();
				this.sendNotification(PlayerCenterConstant.GET_ADD_PLAYER_CELL,{status:true});
				Alert.showErrorTip("增加成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
//				this.sendNotification(PlayerCenterConstant.GET_ADD_PLAYER_CELL,{status:false});
				
			}
		}
		/**
		 *请求保存阵容 
		 * @param $obj
		 * 
		 */
		public function requestsave_battlearray($obj:Object):void
		{
			var arr:Array = $obj.obj;
			var len:int = arr.length;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4 + len * 8 ,MainCommandType.MAINDEAL,SMDPlayerCenter.SAVE_BATTLEARRAY);
			sendBuf.writeByte($obj.formation);
			sendBuf.writeByte($obj.ntype);
			
			sendBuf.writeShort(len);
			var obj:Object;
			for(var i:int = 0;i < len;i++)
			{
				obj = arr[i];
				sendBuf.writeInt(obj.id);
				sendBuf.writeShort(obj.x);
				sendBuf.writeShort(obj.y);
			}
			
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		
		
		/**
		 *返回保存阵容 
		 * @param buffer
		 * 
		 */
		private function getbattlearray(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
			var id:int;
			if(nretrun ==0)
			{
				var formation:int = buffer.readByte();
				var ntype:int = buffer.readByte();
				if(formation == 1)
				{
					_playerCenterData.firstBattlearrayVo.ntype = ntype;
					_playerCenterData.firstBattlearrayVo.changePlace(buffer);
					
				}else
				{
					_playerCenterData.secondBattlearrayVo.ntype = ntype;
					_playerCenterData.secondBattlearrayVo.changePlace(buffer);
				}
				this.sendNotification(PlayerCenterConstant.GET_SAVE_BATTLEARRAY,{status:true});
				Alert.showErrorTip("保存成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
				this.sendNotification(PlayerCenterConstant.GET_SAVE_BATTLEARRAY,{status:false});
				
			}
		}
		/**
		 *请求更换装备  
		 * @param $obj
		 * {playerId:playerId,cellId:cellId,posid:posid,ntype:ntype}
		 */
		public function replace_equip($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 8  ,MainCommandType.MAINDEAL,SMDPlayerCenter.REPLACE_EQUIP);
		
			sendBuf.writeInt($obj.playerId);
			sendBuf.writeShort($obj.cellId);
			sendBuf.writeByte($obj.posid);
			sendBuf.writeByte($obj.ntype);
			
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回装备  
		 * @param $obj
		 * 
		 */
		private function getreplace_equip(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
			var id:int;
			if(nretrun ==0)
			{
				var playerId:int 
				playerId = _playerCenterData.replaceEquip(buffer);
				this.sendNotification(PlayerCenterConstant.GET_REPLACE_EQUIP,{status:true,playerId:playerId});
				Alert.showErrorTip("操作成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
				this.sendNotification(PlayerCenterConstant.GET_REPLACE_EQUIP,{status:false});
				
			}
		}
		
		/**
		 *改变球员号码 
		 * 
		 */
		public function requestChangePlayerNumber($obj:Object):void
		{
			var arr:Array = $obj.arr;
			var len:int = arr.length;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + len * 5 + 2  ,MainCommandType.MAINDEAL,SMDPlayerCenter.CHANGE_PLAYER_NUMBER);
			
			sendBuf.writeShort(len);
			var obj:Object;
			for(var i:int = 0;i < len;i++)
			{
				obj = arr[i];
				sendBuf.writeInt(obj.playerId);
				sendBuf.writeByte(obj.number);
			}
			
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回修改号码 
		 * @param buffer
		 * 
		 */
		private function getChangePlayerNumber(buffer:ByteArray):void
		{
			var nretrun:int = buffer.readInt();
			if(nretrun ==0)
			{
				var len:int = buffer.readShort();
				var id:int ;
				var number:int;
				for(var i:int = 0;i < len;i++)
				{
					id = buffer.readInt();
					number = buffer.readByte();
					
					_playerCenterData.changePlayerNumber(id,number);
					this.sendNotification(PlayerCenterConstant.GET_CHANGE_PLAYER_NUMBER,{status:true,playerId:id});
				}
				
				Alert.showErrorTip("保存成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nretrun));
				this.sendNotification(PlayerCenterConstant.GET_CHANGE_PLAYER_NUMBER,{status:false});
				
			}
		}
			
		/**
		 *获得球员 
		 * {playerId:playerId,starId:starId}
		 */
		public function addPlayer($obj:Object):void
		{
			_playerCenterData.addPlayerVo($obj);
			this.sendNotification(PlayerCenterConstant.GET_ADD_PLAYER,$obj);
		}
		
		
		/**
		 *请求球员体力 
		 * @param $obj
		 * 
		 */
		public function requestPlayerPower($obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.UPDATE_PLAYER_POWER;
			ProxyUtils.instance.webClient(getPlayerPower,variables,false);
		}
		/**
		 *返回球员最新体力 
		 * 
		 */
		private function getPlayerPower($date:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($date,PlayerCenterProxy);
			if(obj.retcode == 0)
			{
				AppData.getInstance().playerCenterDataManager.updatePlayerPhysical(obj.physical);
				this.sendNotification(PlayerCenterConstant.GET_CHANGE_POWERANDBUFF,{status:true,id:0});
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
	}
}